import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: syyyy
 * Date: 2025-11-11
 * Time: 18:33
 */
public class Test {
    //找子集中元素最多的那一个
    public static int findMaxForm(String[] strs, int m, int n) {
        int maxLen=0;
        ArrayList<String> arrayList=new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            String str=strs[i];
            int j = 0;
            while(j < str.length() && m > 0 && n > 0){
                char ch=str.charAt(j);
                if(ch =='1'){
                    n--;
                }else {
                    m--;
                }
                j++;
            }

            if((m == 0 || n == 0)){
                //此时所有的子集长度就是最大的了
                if(j == str.length()){
                    arrayList.add(strs[j]);
                }
                maxLen=Math.max(maxLen, arrayList.size());
                if(i < strs.length-1){//这里还要一个判断条件，但是我实在想不出来了
                    String longest=findLongestString(arrayList);
                    arrayList.remove(longest);
                    for (int k = 0; k < longest.length(); k++) {
                        char ch=longest.charAt(k);
                        if(ch == '1'){
                            n++;
                        }else {
                            m++;
                        }
                    }
                }
            }else {
                arrayList.add(strs[i]);
            }
        }
        return maxLen;
    }

    public static String findLongestString(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null; // 集合为空时返回null
        }

        String longestStr = list.get(0); // 初始化为第一个元素
        int maxLength = longestStr.length(); // 初始最长长度

        // 遍历集合，更新最长字符串
        for (String str : list) {
            int currentLength = str.length();
            if (currentLength > maxLength) {
                maxLength = currentLength;
                longestStr = str; // 发现更长的，更新记录
            }
        }
        return longestStr;
    }
    public static void main(String[] args) {
        String[] strs=new String[]{"10","0001","111001","1","0"};
        int maxLen=findMaxForm(strs,5,3);
        System.out.println(maxLen);
    }
}
